<!DOCTYPE html>
<html lang="en" data-color-mode="auto" data-light-theme="light" data-dark-theme="dark">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>G4F - Legacy API</title>
    <link rel="apple-touch-icon" sizes="180x180" href="/dist/img/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="/dist/img/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="/dist/img/favicon-16x16.png">
    <link rel="manifest" href="/dist/img/site.webmanifest">
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/light-74231a1f3bbb.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/dark-8a995f0bacd4.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-primitives-225433424a87.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-b8b91660c29d.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/global-205098e9fedd.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/code-177d21388df8.css" />
    <style>
        :root {
            --colour-1: #000000;
            --colour-2: #ccc;
            --colour-3: #e4d4ff;
            --colour-4: #f0f0f0;
            --colour-5: #181818;
            --colour-6: #242424;
            --accent: #8b3dff;
            --gradient: #1a1a1a;
            --background: #16101b;
            --size: 70vw;
            --top: 50%;
            --blur: 40px;
            --opacity: 0.6;
        }

        /* Body and text color */
        body {
            height: 100vh;
            margin: 0;
            padding: 0;
        }

        .hidden {
            display: none;
        }

        .container-lg {
            margin: 0 auto;
            padding: 8px;
        }

        @media only screen and (min-width: 40em) {
            .container-lg {
                max-width: 84%;
            }
        }
    </style>
</head>
<body>
    <article class="markdown-body entry-content container-lg" itemprop="text"><div class="markdown-heading"><h2 class="heading-element">G4F - Legacy API</h2><a id="user-content-g4f---legacy-api" class="anchor" aria-label="Permalink: G4F - Legacy API" href="#g4f---legacy-api"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="markdown-heading"><h4 class="heading-element">ChatCompletion</h4><a id="user-content-chatcompletion" class="anchor" aria-label="Permalink: ChatCompletion" href="#chatcompletion"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="highlight highlight-source-python"><pre><span class="pl-k">import</span> <span class="pl-s1">g4f</span>

<span class="pl-s1">g4f</span>.<span class="pl-c1">debug</span>.<span class="pl-c1">logging</span> <span class="pl-c1">=</span> <span class="pl-c1">True</span>  <span class="pl-c"># Enable debug logging</span>
<span class="pl-s1">g4f</span>.<span class="pl-c1">debug</span>.<span class="pl-c1">version_check</span> <span class="pl-c1">=</span> <span class="pl-c1">False</span>  <span class="pl-c"># Disable automatic version checking</span>
<span class="pl-en">print</span>(<span class="pl-s1">g4f</span>.<span class="pl-c1">Provider</span>.<span class="pl-c1">Gemini</span>.<span class="pl-c1">params</span>)  <span class="pl-c"># Print supported args for Gemini</span>

<span class="pl-c"># Using automatic a provider for the given model</span>
<span class="pl-c">## Streamed completion</span>
<span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">g4f</span>.<span class="pl-c1">ChatCompletion</span>.<span class="pl-c1">create</span>(
    <span class="pl-s1">model</span><span class="pl-c1">=</span><span class="pl-s">"gpt-4o"</span>,
    <span class="pl-s1">messages</span><span class="pl-c1">=</span>[{<span class="pl-s">"role"</span>: <span class="pl-s">"user"</span>, <span class="pl-s">"content"</span>: <span class="pl-s">"Hello"</span>}],
    <span class="pl-s1">stream</span><span class="pl-c1">=</span><span class="pl-c1">True</span>,
)
<span class="pl-k">for</span> <span class="pl-s1">message</span> <span class="pl-c1">in</span> <span class="pl-s1">response</span>:
    <span class="pl-en">print</span>(<span class="pl-s1">message</span>, <span class="pl-s1">flush</span><span class="pl-c1">=</span><span class="pl-c1">True</span>, <span class="pl-s1">end</span><span class="pl-c1">=</span><span class="pl-s">''</span>)

<span class="pl-c">## Normal response</span>
<span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">g4f</span>.<span class="pl-c1">ChatCompletion</span>.<span class="pl-c1">create</span>(
    <span class="pl-s1">model</span><span class="pl-c1">=</span><span class="pl-s1">g4f</span>.<span class="pl-c1">models</span>.<span class="pl-c1">gpt_4</span>,
    <span class="pl-s1">messages</span><span class="pl-c1">=</span>[{<span class="pl-s">"role"</span>: <span class="pl-s">"user"</span>, <span class="pl-s">"content"</span>: <span class="pl-s">"Hello"</span>}],
)  <span class="pl-c"># Alternative model setting</span>

<span class="pl-en">print</span>(<span class="pl-s1">response</span>)</pre></div>
<div class="markdown-heading"><h5 class="heading-element">Providers</h5><a id="user-content-providers" class="anchor" aria-label="Permalink: Providers" href="#providers"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="highlight highlight-source-python"><pre><span class="pl-k">import</span> <span class="pl-s1">g4f</span>

<span class="pl-c"># Print all available providers</span>
<span class="pl-en">print</span>([
    <span class="pl-s1">provider</span>.<span class="pl-c1">__name__</span>
    <span class="pl-k">for</span> <span class="pl-s1">provider</span> <span class="pl-c1">in</span> <span class="pl-s1">g4f</span>.<span class="pl-c1">Provider</span>.<span class="pl-c1">__providers__</span>
    <span class="pl-k">if</span> <span class="pl-s1">provider</span>.<span class="pl-c1">working</span>
])

<span class="pl-c"># Execute with a specific provider</span>
<span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">g4f</span>.<span class="pl-c1">ChatCompletion</span>.<span class="pl-c1">create</span>(
    <span class="pl-s1">model</span><span class="pl-c1">=</span><span class="pl-s">"gpt-4o"</span>,
    <span class="pl-s1">provider</span><span class="pl-c1">=</span><span class="pl-s1">g4f</span>.<span class="pl-c1">Provider</span>.<span class="pl-c1">Copilot</span>,
    <span class="pl-s1">messages</span><span class="pl-c1">=</span>[{<span class="pl-s">"role"</span>: <span class="pl-s">"user"</span>, <span class="pl-s">"content"</span>: <span class="pl-s">"Hello"</span>}],
    <span class="pl-s1">stream</span><span class="pl-c1">=</span><span class="pl-c1">True</span>,
)
<span class="pl-k">for</span> <span class="pl-s1">message</span> <span class="pl-c1">in</span> <span class="pl-s1">response</span>:
    <span class="pl-en">print</span>(<span class="pl-s1">message</span>)</pre></div>
<div class="markdown-heading"><h5 class="heading-element">Image Upload &amp; Generation</h5><a id="user-content-image-upload--generation" class="anchor" aria-label="Permalink: Image Upload &amp; Generation" href="#image-upload--generation"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p>Image upload and generation are supported by three main providers:</p>
<ul>
<li>
<strong>Microsoft Copilot &amp; Other GPT-4 Providers:</strong> Utilizes Microsoft's Image Creator.</li>
<li>
<strong>Google Gemini:</strong> Available for free accounts with IP addresses outside Europe.</li>
<li>
<strong>OpenaiChat with GPT-4:</strong> Accessible for users with a Plus subscription.</li>
</ul>
<div class="highlight highlight-source-python"><pre><span class="pl-k">import</span> <span class="pl-s1">g4f</span>

<span class="pl-c"># Setting up the request for image creation</span>
<span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">g4f</span>.<span class="pl-c1">ChatCompletion</span>.<span class="pl-c1">create</span>(
    <span class="pl-s1">model</span><span class="pl-c1">=</span><span class="pl-s1">g4f</span>.<span class="pl-c1">models</span>.<span class="pl-c1">default</span>, <span class="pl-c"># Using the default model</span>
    <span class="pl-s1">provider</span><span class="pl-c1">=</span><span class="pl-s1">g4f</span>.<span class="pl-c1">Provider</span>.<span class="pl-c1">Gemini</span>, <span class="pl-c"># Specifying the provider as Gemini</span>
    <span class="pl-s1">messages</span><span class="pl-c1">=</span>[{<span class="pl-s">"role"</span>: <span class="pl-s">"user"</span>, <span class="pl-s">"content"</span>: <span class="pl-s">"Create an image like this"</span>}],
    <span class="pl-s1">image</span><span class="pl-c1">=</span><span class="pl-en">open</span>(<span class="pl-s">"images/g4f.png"</span>, <span class="pl-s">"rb"</span>), <span class="pl-c"># Image input can be a data URI, bytes, PIL Image, or IO object</span>
    <span class="pl-s1">image_name</span><span class="pl-c1">=</span><span class="pl-s">"g4f.png"</span> <span class="pl-c"># Optional: specifying the filename</span>
)

<span class="pl-c"># Displaying the response</span>
<span class="pl-en">print</span>(<span class="pl-s1">response</span>)

<span class="pl-k">from</span> <span class="pl-s1">g4f</span>.<span class="pl-s1">image</span> <span class="pl-k">import</span> <span class="pl-v">ImageResponse</span>

<span class="pl-c"># Get image links from response</span>
<span class="pl-k">for</span> <span class="pl-s1">chunk</span> <span class="pl-c1">in</span> <span class="pl-s1">g4f</span>.<span class="pl-c1">ChatCompletion</span>.<span class="pl-c1">create</span>(
    <span class="pl-s1">model</span><span class="pl-c1">=</span><span class="pl-s1">g4f</span>.<span class="pl-c1">models</span>.<span class="pl-c1">default</span>, <span class="pl-c"># Using the default model</span>
    <span class="pl-s1">provider</span><span class="pl-c1">=</span><span class="pl-s1">g4f</span>.<span class="pl-c1">Provider</span>.<span class="pl-c1">OpenaiChat</span>, <span class="pl-c"># Specifying the provider as OpenaiChat</span>
    <span class="pl-s1">messages</span><span class="pl-c1">=</span>[{<span class="pl-s">"role"</span>: <span class="pl-s">"user"</span>, <span class="pl-s">"content"</span>: <span class="pl-s">"Create images with dogs"</span>}],
    <span class="pl-s1">stream</span><span class="pl-c1">=</span><span class="pl-c1">True</span>,
    <span class="pl-s1">ignore_stream</span><span class="pl-c1">=</span><span class="pl-c1">True</span>
):
    <span class="pl-k">if</span> <span class="pl-en">isinstance</span>(<span class="pl-s1">chunk</span>, <span class="pl-v">ImageResponse</span>):
        <span class="pl-en">print</span>(<span class="pl-s1">chunk</span>.<span class="pl-c1">urls</span>) <span class="pl-c"># Print generated image links</span>
        <span class="pl-en">print</span>(<span class="pl-s1">chunk</span>.<span class="pl-c1">alt</span>) <span class="pl-c"># Print used prompt for image generation</span></pre></div>
<div class="markdown-heading"><h5 class="heading-element">Async Support</h5><a id="user-content-async-support" class="anchor" aria-label="Permalink: Async Support" href="#async-support"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p>To enhance speed and overall performance, execute providers asynchronously. The total execution time will be determined by the duration of the slowest provider's execution.</p>
<div class="highlight highlight-source-python"><pre><span class="pl-k">import</span> <span class="pl-s1">g4f</span>
<span class="pl-k">import</span> <span class="pl-s1">asyncio</span>

<span class="pl-s1">_providers</span> <span class="pl-c1">=</span> [
    <span class="pl-s1">g4f</span>.<span class="pl-c1">Provider</span>.<span class="pl-c1">Copilot</span>,
    <span class="pl-s1">g4f</span>.<span class="pl-c1">Provider</span>.<span class="pl-c1">PollinationsAI</span>,
    <span class="pl-s1">g4f</span>.<span class="pl-c1">Provider</span>.<span class="pl-c1">Gemini</span>,
]

<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">run_provider</span>(<span class="pl-s1">provider</span>: <span class="pl-s1">g4f</span>.<span class="pl-c1">Provider</span>.<span class="pl-c1">BaseProvider</span>):
    <span class="pl-k">try</span>:
        <span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">g4f</span>.<span class="pl-c1">ChatCompletion</span>.<span class="pl-c1">create_async</span>(
            <span class="pl-s1">model</span><span class="pl-c1">=</span><span class="pl-s1">g4f</span>.<span class="pl-c1">models</span>.<span class="pl-c1">default</span>,
            <span class="pl-s1">messages</span><span class="pl-c1">=</span>[{<span class="pl-s">"role"</span>: <span class="pl-s">"user"</span>, <span class="pl-s">"content"</span>: <span class="pl-s">"Hello"</span>}],
            <span class="pl-s1">provider</span><span class="pl-c1">=</span><span class="pl-s1">provider</span>,
        )
        <span class="pl-en">print</span>(<span class="pl-s">f"<span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">provider</span>.<span class="pl-c1">__name__</span><span class="pl-kos">}</span></span>:"</span>, <span class="pl-s1">response</span>)
    <span class="pl-k">except</span> <span class="pl-v">Exception</span> <span class="pl-k">as</span> <span class="pl-s1">e</span>:
        <span class="pl-en">print</span>(<span class="pl-s">f"<span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">provider</span>.<span class="pl-c1">__name__</span><span class="pl-kos">}</span></span>:"</span>, <span class="pl-s1">e</span>)
        
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">run_all</span>():
    <span class="pl-s1">calls</span> <span class="pl-c1">=</span> [
        <span class="pl-en">run_provider</span>(<span class="pl-s1">provider</span>) <span class="pl-k">for</span> <span class="pl-s1">provider</span> <span class="pl-c1">in</span> <span class="pl-s1">_providers</span>
    ]
    <span class="pl-k">await</span> <span class="pl-s1">asyncio</span>.<span class="pl-c1">gather</span>(<span class="pl-c1">*</span><span class="pl-s1">calls</span>)

<span class="pl-s1">asyncio</span>.<span class="pl-c1">run</span>(<span class="pl-en">run_all</span>())</pre></div>
<div class="markdown-heading"><h5 class="heading-element">Proxy and Timeout Support</h5><a id="user-content-proxy-and-timeout-support" class="anchor" aria-label="Permalink: Proxy and Timeout Support" href="#proxy-and-timeout-support"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p>All providers support specifying a proxy and increasing timeout in the create functions.</p>
<div class="highlight highlight-source-python"><pre><span class="pl-k">import</span> <span class="pl-s1">g4f</span>

<span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">g4f</span>.<span class="pl-c1">ChatCompletion</span>.<span class="pl-c1">create</span>(
    <span class="pl-s1">model</span><span class="pl-c1">=</span><span class="pl-s1">g4f</span>.<span class="pl-c1">models</span>.<span class="pl-c1">default</span>,
    <span class="pl-s1">messages</span><span class="pl-c1">=</span>[{<span class="pl-s">"role"</span>: <span class="pl-s">"user"</span>, <span class="pl-s">"content"</span>: <span class="pl-s">"Hello"</span>}],
    <span class="pl-s1">proxy</span><span class="pl-c1">=</span><span class="pl-s">"http://host:port"</span>,
    <span class="pl-c"># or socks5://user:pass@host:port</span>
    <span class="pl-s1">timeout</span><span class="pl-c1">=</span><span class="pl-c1">120</span>,  <span class="pl-c"># in secs</span>
)

<span class="pl-en">print</span>(<span class="pl-s">f"Result:"</span>, <span class="pl-s1">response</span>)</pre></div>
<hr>
<p><a href="/docs/">Return to Documentation</a></p>
</article>
</body>
</html>